package com.hamster.crmpre.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hamster.crmpre.mapper.*;
import com.hamster.crmpre.pojo.*;
import com.hamster.crmpre.service.SchemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 方案执行率的Service
 * @author Tarry
 * @create 2019/12/31 11:29
 */
@Service
public class SchemeServiceImpl implements SchemeService {
    @Autowired
    private SchemeMapper schemeMapper;
    @Autowired
    private SchemeConfigMapper schemeConfigMapper;
    @Autowired
    private SchemeEmployeeMapper schemeEmployeeMapper;
    @Autowired
    private SchemeResultMapper schemeResultMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    /**
     * 查询 执行方案 列表
     * @return
     */
    @Override
    public List<Scheme> listSchemes() {
        QueryWrapper<Scheme> schemeQueryWrapper = new QueryWrapper<>();
        schemeQueryWrapper.eq("state","1").eq("del","0");
        schemeQueryWrapper.orderByDesc("create_time");
        return schemeMapper.selectList(schemeQueryWrapper);
    }

    /**
     * 查询 执行方案 详细的内容
     * @param employeeId
     * @param schemeId
     */
    @Override
    public SchemeEmployee getSchemeDetails(String employeeId, String schemeId) {
        QueryWrapper<SchemeEmployee> schemeEmployeeQueryWrapper = new QueryWrapper<>();
        schemeEmployeeQueryWrapper.eq("employee_id",employeeId).eq("scheme_id",schemeId);
        SchemeEmployee schemeEmployee = schemeEmployeeMapper.selectOne(schemeEmployeeQueryWrapper);
        //如果不存在则新增
        if (schemeEmployee == null){
            Employee employee = employeeMapper.selectById(employeeId);
            Scheme scheme = schemeMapper.selectById(schemeId);
            SchemeEmployee newScheme = new SchemeEmployee();
            newScheme.setEmployeeId(employeeId);//员工id
            newScheme.setEmployeeName(employee.getName());//员工姓名
            newScheme.setImplementationRate(0.0);
            newScheme.setSchemeCreateTime(scheme.getCreateTime());
            newScheme.setSchemeEndTime(scheme.getEndTime());
            newScheme.setSchemeId(schemeId);
            newScheme.setSchemeStartTime(scheme.getStartTime());
            newScheme.setSchemeTitle(scheme.getTitle());
            newScheme.setTargetPopulation(scheme.getTargetPopulation());
            newScheme.setState(scheme.getState());
            schemeEmployeeMapper.insert(newScheme);
            schemeEmployee = schemeEmployeeMapper.selectById(newScheme.getId());//查询并赋值
        }
        QueryWrapper<SchemeResult> schemeResultQueryWrapper = new QueryWrapper<>();
        schemeResultQueryWrapper.eq("scheme_employee_id",schemeEmployee.getId());
        List<SchemeResult> schemeResults = schemeResultMapper.selectList(schemeResultQueryWrapper);
        schemeEmployee.setResultList(schemeResults);
        return schemeEmployee;
    }

    /**
     * 查询该员工手下的所以客户列表
     * @param userId
     * @return
     */
    @Override
    public List<Customer> listCustomers(String userId) {
        //本功能只针对vip，所以只查询vip客户
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","name","phone");
        queryWrapper.eq("principal_vip_id",userId);
        return customerMapper.selectList(queryWrapper);
    }

    /**
     * 获取方案执行率设置
     * @return
     */
    @Override
    public List<SchemeConfig> listConfig(){
        QueryWrapper<SchemeConfig> schemeConfigQueryWrapper = new QueryWrapper<>();
        schemeConfigQueryWrapper.eq("del",0);
        return schemeConfigMapper.selectList(schemeConfigQueryWrapper);
    }


    /**
     * 保存内容
     * @param schemeEmployee
     */
    @Override
    @Transactional
    public void insertScheme(SchemeEmployee schemeEmployee) {
        String id = schemeEmployee.getId();
        List<SchemeResult> resultList = schemeEmployee.getResultList();
        //先删除，再添加
        schemeResultMapper.deleteScheme(id);
        //添加
        for (SchemeResult schemeResult : resultList) {
            schemeResult.setSchemeEmployeeId(id); //改客户属于哪个员工的哪个方案的id
            Customer customer = customerMapper.selectById(schemeResult.getCustomerId());
            schemeResult.setCustomerName(customer.getName());
            schemeResult.setCustomerPhone(customer.getPhone());
            schemeResult.setSchemeId(schemeEmployee.getSchemeId());//方案id
            //拿到执行进度 id ，去填写 进步标题，执行率
            SchemeConfig schemeConfig = schemeConfigMapper.selectById(schemeResult.getProcessId());
            schemeResult.setProcessTitle(schemeConfig.getExecutingState());//执行情况名称
            schemeResult.setImplementationRate(schemeConfig.getImplementationRate());//执行率

            schemeResultMapper.insert(schemeResult);
        }


        // 计算方案执行率
        List<Double> list = new ArrayList<>();
        for (SchemeResult schemeResult : resultList) {
            String processId = schemeResult.getProcessId();
            Double implementationRate = schemeConfigMapper.selectById(processId).getImplementationRate();
            list.add(implementationRate);
        }
        Double sum = 0.0;
        for (Double aDouble : list) {
            sum += aDouble;
        }
        schemeEmployee.setImplementationRate(sum/list.size());//设置最新的方案执行率
        schemeEmployeeMapper.updateById(schemeEmployee);

    }
}
